<template>
  <div class="p20 bg-white console relative">
    <div class="mt10 mb20">
      <el-button
        class="theme-btn"
        @click="handleAdd"
        v-if="permitIds.includes('info_adv_add')"
        >新增公告</el-button
      >
      <span class="ml10">
        <i class="label">发布时间：</i>
        <el-date-picker
          type="date"
          class="filter-time"
          placeholder="选择时间"
          v-model="createDateFrom"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </span>
      <span class="ml10">
        <i class="label">至：</i>
        <el-date-picker
          type="date"
          class="filter-time"
          placeholder="选择时间"
          v-model="createDateTo"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </span>
      <span class="ml10">
        <i class="label">状态：</i>
        <el-select
          v-model="status"
          @change="handleUpdate"
          clearable
          class="small-select"
        >
          <el-option label="撤回" value="0"></el-option>
          <el-option label="发布" value="1"></el-option>
        </el-select>
      </span>
      <span class="ml10">
        <i class="label">公告类型：</i>
        <el-select
          clearable
          class="small-select"
          @change="handleUpdate"
          v-model="announcementType"
        >
          <el-option
            :key="item.number"
            :label="item.name"
            :value="item.number"
            v-for="item in announcementTypeList"
          ></el-option>
        </el-select>
      </span>
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
      <div
        class="fr"
        style="margin-top: -80px;"
        v-if="permitIds.includes('info_adv_export')"
      >
        <el-button @click="handlExportExcel">导出</el-button>
      </div>
    </div>
    <el-table
      border
      stripe
      :data="tableData"
      v-loading="loading"
      @selection-change="handlSelect"
      element-loading-text="拼命加载中"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="title" label="公告标题">
        <template slot-scope="scope">
          <span>
            <i v-if="scope.row.top === 2" class="theme">[置顶]</i>
            {{ scope.row.title }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="发布人"></el-table-column>
      <el-table-column
        prop="announcementType"
        label="公告类型"
      ></el-table-column>
      <el-table-column prop="createDate" label="发布时间"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="operation" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            class="theme-btn"
            @click="handleDetail(scope.row.id)"
            v-if="permitIds.includes('info_adv_load')"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id)"
            v-if="permitIds.includes('info_adv_update')"
            >修改</el-button
          >
          <el-button
            size="mini"
            @click="handleDelete(scope.row.id)"
            v-if="permitIds.includes('info_adv_delete')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";
import { getAllDictionary } from "@/apis/dictionary";
import { getAllInforelease, deleteNotice } from "@/apis/information";

export default {
  name: "Community_notice",
  data() {
    return {
      status: "",
      pageTotal: 0,
      tableData: [],
      createDateTo: "",
      createDateFrom: "",
      exportExcelData: [],
      announcementType: "",
      announcementTypeList: [],
      placeholder: "输入关键字搜索",
      tHeader: ["公告标题", "发布人", "公告类型", "发布时间", "状态"],
      filterVal: [
        "title",
        "realName",
        "announcementType",
        "createDate",
        "status"
      ]
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.getAnnouncementTypeList();
    this.$store.dispatch("pagination/resetPageNum");
    this.getInforeleases(this.pageSize, this.pageNum);
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "queryStr", "permitIds", "loading"])
  },
  methods: {
    async getAnnouncementTypeList() {
      const { rows } = await getAllDictionary({
        fieldName: "announcementType"
      });

      this.announcementTypeList = rows;
    },
    async getInforeleases(pageSize, pageNum) {
      const { object } = await getAllInforelease({
        pageNum,
        pageSize,
        status: this.status,
        queryStr: this.queryStr,
        createDateTo: this.createDateTo,
        createDateFrom: this.createDateFrom,
        announcementType: this.announcementType
      });

      this.pageTotal = object.total;
      this.tableData = object.records;
      this.$store.dispatch("load/setLoading", false);
    },
    handleUpdate() {
      this.getInforeleases(this.pageSize, this.pageNum);
    },
    handleAdd() {
      this.$router.push({ name: "addNotice" });
    },
    handleDetail(id) {
      this.$router.push({
        name: "detailNotice",
        query: {
          id
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // deleteNotice({ communityAnnouncementId: id }).then(res => {
          //   this.$notify({
          //     title: res.msg,
          //     type: "success"
          //   });
          //   this.$store.dispatch("load/setLoading", false);
          //   this.handleUpdate();
          // });
          this.$notify({
            title: "操作成功!",
            type: "success"
          });
          this.$store.dispatch("load/setLoading", false);
          this.handleUpdate();
        })
        .catch(() => {
          this.$message("已取消删除");
        });
    },
    handlExportExcel() {
      if (!this.exportExcelData.length) {
        this.$confirm("是否导出所有数据？", {
          distinguishCancelAndClose: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(_ => {
            const params = {
              pageNum: 1,
              status: this.status,
              queryStr: this.queryStr,
              pageSize: this.pageTotal,
              createDateTo: this.createDateTo,
              createDateFrom: this.createDateFrom,
              announcementType: this.announcementType
            };

            // getAllInforelease(params).then(res => {
            //   this.$store.dispatch("exportExcel/setExportExcel", {
            //     tHeader: this.tHeader,
            //     filterVal: this.filterVal,
            //     exportExcelData: res.object.records
            //   });
            // });
            this.$store.dispatch("exportExcel/setExportExcel", {
              tHeader: this.tHeader,
              filterVal: this.filterVal,
              exportExcelData: this.exportExcelData
            });
          })
          .catch(action => {
            this.$message({
              type: "info",
              message: "取消导出所有操作"
            });
          });
      } else {
        this.$store.dispatch("exportExcel/setExportExcel", {
          tHeader: this.tHeader,
          filterVal: this.filterVal,
          exportExcelData: this.exportExcelData
        });
      }
    },
    handleEdit(id) {
      this.$router.push({
        name: "editNotice",
        query: {
          id
        }
      });
    },
    handlSelect(selection) {
      this.exportExcelData = selection;
    }
  },
  components: {
    Search,
    Pagination
  }
};
</script>

<style scoped lang="less"></style>
